package com.bdsys.serviceimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bdsys.dao.DeviceDAO;
import com.bdsys.dao.ImageDAO;
import com.bdsys.domain.DeviceDomain;
import com.bdsys.domain.ImageDomain;
import com.bdsys.service.DeviceService;
import com.bdsys.servicedto.DeviceDetailDTO;
import com.bdsys.servicedto.DeviceServiceDTO;

public class DeviceServiceImpl implements DeviceService{
	
	DeviceDAO deviceDAO;
	
	public void setImageDAO(ImageDAO imageDAO) {
		this.imageDAO = imageDAO;
	}

	ImageDAO imageDAO;
	
	public void setDeviceDAO(DeviceDAO deviceDAO){
		this.deviceDAO = deviceDAO;
	}
	
	@Override
	public void addDevice(DeviceServiceDTO inServiceDTO){
		deviceDAO.addDevice(this.createDeviceDAOParameter(inServiceDTO));
		
		List<ImageDomain> imageDomains = this.fromDetailDTOTOImageDomain(inServiceDTO.getDeviceDetailDTO());
		for(ImageDomain imageDomain : imageDomains){
			imageDAO.addImage(imageDomain);
		}
	}

	@Override
	public DeviceServiceDTO selectByConditions(DeviceServiceDTO inServiceDTO) {
		List<DeviceDomain> list = deviceDAO.selectByConditions(this.createDeviceDAOParameter(inServiceDTO));
		
		List<DeviceDetailDTO> outList = new ArrayList<DeviceDetailDTO>();
		for(DeviceDomain outDomain : list){
			DeviceDetailDTO  outDetailDTO = this.fromDomainToDetailDTO(outDomain);
			outList.add(outDetailDTO);
		}
		
		DeviceServiceDTO outServiceDTO = new DeviceServiceDTO();
		outServiceDTO.setList(outList);
		return outServiceDTO;
	}
	
	@Override
	public void deleteDevice(DeviceServiceDTO inServiceDTO) {
		DeviceDomain inDomain = this.createDeviceDAOParameter(inServiceDTO);
		deviceDAO.deleteDevice(inDomain);
		imageDAO.deleteImage(inDomain.getCode());
	}
	
	@Override
	public void updateDevice(DeviceServiceDTO inServiceDTO) {
		deviceDAO.updateDevice(this.createDeviceDAOParameter(inServiceDTO));
	}
	
	private DeviceDomain createDeviceDAOParameter(DeviceServiceDTO inServiceDTO){
		DeviceDetailDTO inDetailDTO = inServiceDTO.getDeviceDetailDTO();
		DeviceDomain inDomain = this.fromDetailDTOToDeviceDomain(inDetailDTO);
		return inDomain;
	}
	
	private DeviceDomain fromDetailDTOToDeviceDomain(DeviceDetailDTO detailDTO){
		DeviceDomain deviceDomain = new DeviceDomain();
		deviceDomain.setCode(detailDTO.getCode());
		deviceDomain.setName(detailDTO.getName());
		deviceDomain.setTimeType(detailDTO.getTimeType());
		deviceDomain.setAvailable(detailDTO.getAvailable());
		deviceDomain.setDescription(detailDTO.getDescription());
		return deviceDomain;
	}
	
	private List<ImageDomain> fromDetailDTOTOImageDomain(DeviceDetailDTO detailDTO){
		List<ImageDomain> imageDomians = new ArrayList<ImageDomain>();
		

		Map<String, byte[]> idFile = detailDTO.getIdFile();
		
		Map<String, String> idName = detailDTO.getIdName();
		Map<String, String> idDescription = detailDTO.getIdDescription();
		Set<String> ids = idFile.keySet();
		
		for(String id:ids){
			ImageDomain imageDomain = new ImageDomain();
			if(detailDTO.getIdIsShowImage().containsKey(id)){
				imageDomain.setDefalut(detailDTO.getIdIsShowImage().get(id));
			}else{
				imageDomain.setDefalut("0");
			}
			imageDomain.setDid(detailDTO.getCode());
			
			imageDomain.setContent(idFile.get(id));
			imageDomain.setDescription(idDescription.get(id));
			imageDomain.setName(idName.get(id));
			imageDomians.add(imageDomain);
		}
		
		return imageDomians;
	}
	
	private DeviceDetailDTO fromDomainToDetailDTO(DeviceDomain deviceDomain){
		DeviceDetailDTO deviceDetailDTO = new DeviceDetailDTO();
		deviceDetailDTO.setCode(deviceDomain.getCode());
		deviceDetailDTO.setName(deviceDomain.getName());
		deviceDetailDTO.setTimeType(deviceDomain.getTimeType());
		deviceDetailDTO.setAvailable(deviceDomain.getAvailable());
		deviceDetailDTO.setDescription(deviceDomain.getDescription());
		return deviceDetailDTO;
	}
}
